package learnkotlin


/**
 * kotlin  面向对象编程
 */
object KotlinDemo2 {
    @JvmStatic
    fun main(args: Array<String>) {
        val p = PerSon()
        p.name = "张三"
        p.age = 45
        p.eat()

        val student = Student("34565", 8)
        val student2 = Student2("355334", 6, "李四", 12)

        val student3 = Student3()
        doStudy(student3)

        val cellPhone = CellPhone("HUAWei", 4999.9)
        val cellPhone2 = CellPhone("HUAWei", 4999.9)
        println(cellPhone == cellPhone2) //没加 data 前是false

        SingleTon.run()

    }

    /**
     * 继承:  第一 open 声明类可以被继承  第二:  :父类()表示继承
     */
    open class PerSon {
        var name = ""
        var age = 0
        fun eat() {
            println("" + age + "岁的" + name + "喜欢吃 ")
        }
    }

    /**
     * ==============================主构造函数
     *
     */

    class StudentDemo : PerSon() {

    }

    class Student(sno: String, grade: Int) : PerSon() { //主构造函数  1:没有函数体 2:定义在类名后

        init {//相当于在主构造函数中初始化操作了
            println("sno is " + sno)
        }

        fun learning() {
        }
    }

    open class PerSon2(var name: String, var age: Int) {
        fun eat() {
            println("" + age + "岁的" + name + "喜欢吃 ")
        }
    }

    class Student2(val sno: String, val grade: Int, name: String, age: Int) : PerSon2(name, age) {
        init {
            println(name + "的年龄是" + age + " 学号是 " + sno + " 年级是 " + grade)
        }
    }

    /**
     * ==============================  次构造函数  constructor
     */


    /**
     * ================ 接口 interface
     */

    interface Study {
        fun readBooks() {  //默认实现 可实现 可不实现
            println("我是默认实现")
        }

        fun readEnglish()

        fun doHomeWork()
    }

    interface Play {
        fun playGame()
    }

    class Student3 : PerSon(), Study, Play { //继承 类名()   实现:没有括号
//        override fun readBooks() {
//            TODO("Not yet implemented")
//        }

        override fun readEnglish() {
            TODO("Not yet implemented")
        }

        override fun doHomeWork() {
            println("Student3 doHomeWork")
        }

        override fun playGame() {
            TODO("Not yet implemented")
        }
    }

    fun doStudy(study: Study) { //父类引用  传入的是子类对象  面向接口编程  多态
        study.doHomeWork()
    }

    /**
     * ================ 数据类 data 关键字 自动生成 equals hashcode toString 方法
     */

    data class CellPhone(val brand: String, val price: Double) {}


    object SingleTon {
        fun run() {
            println("singleTon  's  run ")
        }
    }

}